#include "RegionManager.hpp"

RegionManager::RegionManager():ObjectManager(){
    size = 256;
    regions = new Region*[size];
    for(unsigned short i = 0; i < size; i++){
        regions[i] = 0;
    }
}

/**
 * Run the onentry lua script that should be triggered when an Entity first
 * enters a Region. This method checks whether the region has an exit script and
 * if it should be called.
 * @param region Region entered.
 * @param entity Entity that entered region.
 */
void RegionManager::onentry(Region* region, Scriptable* entity){
    if(region->hasScript && region->hasEntry)
        runScript("onentry", region, entity);
}

/**
 * Run the onexit lua script that should be triggered when an Entity leaves
 * a Region. This method checks whether the region has an exit script and
 * if it should be called.
 * @param region Region exited.
 * @param entity Entity that left the Region.
 */
void RegionManager::onexit(Region* region, Scriptable* entity){
    if(region->hasScript && region->hasExit)
        runScript("onexit", region, entity);
}

/**
 * Find a region by name.
 * @param name Name to search by.
 * @return Pointer to matching Region or null.
 */
Region* RegionManager::findRegion(const char* name){
    for(int i = 0; i < size; i++){
        if(regions[i] && strcmp(regions[i]->name, name) == 0){
            return regions[i];
        }
    }
    return 0;
}